package com.example.myapplication;

import android.Manifest;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.CallLog;
import android.provider.ContactsContract;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;

import java.util.List;

public class ContactsActivity extends AppCompatActivity {

    private ContactSQLiteOpenHelper contactSQLiteOpenHelper;
    private SQLiteDatabase sqLiteDatabase;


    //读取短信
    private void readSms(){
        Uri uri = Uri.parse("content://sms/");
        String[] strings = new String[]{"_id","address","person","body","date","type"};
        Cursor cursor = getContentResolver().query(uri,strings,null,null,"date desc");
        while (cursor.moveToNext()){
            int _id = cursor.getInt(cursor.getColumnIndex("_id"));
            String address = cursor.getString(cursor.getColumnIndex("address"));
            String person = cursor.getString(cursor.getColumnIndex("person"));
            String body = cursor.getString(cursor.getColumnIndex("body"));
            int type = cursor.getInt(cursor.getColumnIndex("type"));
            String typeText = "接收";
            if (type == 0) typeText = "所有";
            if (type == 1) typeText = "接收";
            if (type == 2) typeText = "发送";
            if (type == 3) typeText = "草稿箱";
            if (type == 4) typeText = "发件箱";
            if (type == 5) typeText = "失败";
            if (type == 6) typeText = "待发送";
            Log.d("TAGS",address+ "|" + body + "|" + typeText );
        }
    }
    //读取通话记录
    private void readCallLog(){
        String[] strings = new String[]{
                CallLog.Calls.CACHED_NAME,//名字
                CallLog.Calls.NUMBER,//号码
                CallLog.Calls.DATE,//时间
                CallLog.Calls.DURATION,//通话时长
                CallLog.Calls.TYPE,//通话类型
        };
        Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI,strings,null,null);
        while (cursor.moveToNext()){
            String name = cursor.getString(0);
            String number = cursor.getString(1);
            String date = cursor.getString(2);
            String duration = cursor.getString(3);
            int type = cursor.getInt(4);
            String tyoeText = "未知的";
            switch (type){
                case CallLog.Calls.INCOMING_TYPE:tyoeText = "来电"; break;
                case CallLog.Calls.OUTGOING_TYPE:tyoeText = "拨通"; break;
                case CallLog.Calls.MISSED_TYPE:tyoeText = "未接"; break;
            }
            Log.d("TAGS", name + "|" + number + "|" + date + "|" + duration + "|" + tyoeText);
        }
    }
    
    @Override
    protected void onPostResume() {
        super.onPostResume();
        XXPermissions.with(this).permission(getPermissions1()).request(new OnPermissionCallback() {
            @Override
            public void onGranted(List<String> permissions, boolean all) {
                readCallLog();
               readSms();
            }

            @Override
            public void onDenied(List<String> permissions, boolean never) {

            }
        });


    }
    private String[] getPermissions1() {
        return  new String[]{
                Manifest.permission.READ_SMS,
                Manifest.permission.READ_CALL_LOG,
                Manifest.permission.READ_CONTACTS,
        };
    }





    private void uploadContacts() {
        Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        while (cursor.moveToNext()) {
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).replace(" ", " ");
            Log.d("TAGS", name + " " + number);
        }
        cursor.close();
    }


}
